<% @body_tag = 'onload="initMap()"' %>

<div id="map" style="width: <%= @gmap[:width] %>px; height: <%= @gmap[:height] %>px"></div>
<% if @gmap[:locator] %>
  <div id="message"></div>
<% end
 def nl2br(s)
     s.gsub(/\n/, '<br/>')
 end

 def nl3br(s)
   s.gsub(/\r/,'<br/>')
 end
%>
<script src="http://maps.google.com/maps?file=api&v=1&key=<%= @gmap[:key] %>" type="text/javascript"></script>

<script type="text/javascript">
//<![CDATA[

function initMap() {

  // Create a base icon for all of our markers that specifies the
    // shadow, icon dimensions, etc.
    var baseIcon = new GIcon();
    if(img)
       baseIcon.shadow= img_sh;
   else
      baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
  
    //  baseIcon.Image = "http://www.google.com/mapfiles/marker.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    baseIcon.infoShadowAnchor = new GPoint(18, 25);

  // Center the map
  var map = new GMap(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.centerAndZoom(new GPoint(<%= @gmap[:center] %>), <%= @gmap[:zoom] %>);

  // Creates a marker whose info window displays the given number
  function createMarker(point, icon_image, text, url,n_img) {
    var icon = new GIcon(baseIcon);
  //  alert(icon_image);
  //  alert(point);
  //  alert(text);
  //  alert(url);
    if (icon_image && icon_image.length > 0) {
      icon.image = icon_image;
    } else {
      icon.image = "http://www.google.com/mapfiles/marker.png";
    }

    var marker = new GMarker(point,icon);

        // Show this marker's index in the info window when it is clicked
        if (text.length > 0) {
          if (url.length > 0) {
             var ntext ;
              String.replace(text,ntext,'\n')
              var html = "<a href='" + url + "'>" + ntext + "</a><br/>";
              
            } else {
              var html = text;
            }
            html = '<div style="white-space:nowrap;">' + html + '</div>';
            if(n_img !=null){
                  html +="<img src=\""+n_img;
                  html +="\" height=\"200\" width=\"240\" >";
            
              }
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
      }
    return marker;
  }

    GEvent.addListener(map, "click", function(overlay, point) {
        var latLngStr = 'new GPoint(' + point.x + ', ' + point.y + ');';
        document.getElementById("message").innerHTML = latLngStr;
  });


    GEvent.addListener( map, "moveend", function() {
          var center = map.getCenter();
          var zoom = map.getZoom();
          var form  = $('new_map');
          var a=1;
          if(form==null){
            form = $('new_tourist');
            a =1;
          }
          if(form==null){
             form = $('new_restaurant');
             a =1;
          }
          if(form==null){
             form = $('new_resident');
             a =1;
          }
          
          if(form==null){
            form = $('rest_map');
             a =1;
          }
          if(form!=null){
           var text  = form.getInputs('hidden');
           text[0+a].value = center.lat().toString();
           text[1+a].value = center.lng().toString();
           //text[3].value = zoom.toString();
          }
        // document.getElementById("message").innerHTML = center.toString();
    });
  <%-

  n = 1
  for marker in @gmap[:markers]
      icon_image = image_path("marker#{n}.png")
    n += 1
     if marker[:point].length > 4
  -%>
      if(img)
        img_x = img_p;
      else
        img_x= "<%= icon_image %>";
      var point = new GPoint(<%=marker[:point]%>);
<% tx = nl3br(marker[:text].to_s)
         %>
      var marker = createMarker(point, img_x, "<p><%= nl2br(tx) %></p>", "<%= marker[:url] %>");
      map.addOverlay(marker);
    <%-
    end
    end
    -%>
 var new_img;
 var tx;
   <%-
   if @gmap[:maker]
  n = 2
  for mk in @gmap[:maker]
      icon_image = image_path("marker#{n}.png")
    n += 1
    #puts mk.long
    if mk.long.to_f.abs > 0
       if mk.img != nil
          @mugshots = Mugshot.find_all_by_filename(mk.img)
           for mugshot in @mugshots do
            %>
          new_img = "<%=mugshot.public_filename%>";
          <%
          end
       end

  -%>
      if(img)
        img_x = img_p;
      else
        img_x= "<%= icon_image %>";
      var point = new GPoint(<%=  mk.long+","+mk.lat %>);
<% tx = mk[:detail].to_s.strip %>
  <% n_tx = nl2br(tx)  + "sss" %>
    tx = "<%=  nl3br(n_tx) %>";
  <% puts n_tx+"----------------------" %>
      var marker = createMarker(point, img_x,tx, "<%= mk[:url] %>",new_img);
      map.addOverlay(marker);
    <%-
      end
    end
    -%>
  <% end %>
 }

 var id_plan;

 function selectplan(id){
   this.id_plan =id;
 }
 function addtolistplan(user,id_subtourist,type) {
	var url    = '/plans/'+id_plan+'/listplans';
          var form  = $('new_map');
          if(form==null)
            form  = $('rest_map');
          if(form!=null)
           var text  = form.getInputs('hidden');
         
        //authenticity_token	E67feqsxc3UhSjRdE38XC4q/v5mRT35U8QsDLBO1npA=
        //commit	Create
        //listplan[plan_id]	1
        //listplan[subtourist_id]	1
     //alert(id_plan);
     //   alert(text[0].value );
	var pars   = 'authenticity_token=' + text[0].value + '&commit=Create';
            pars  += '&listplan[plan_id]='+this.id_plan+'&listplan[subtourist_id]='+id_subtourist;
            pars  += '&listplan[typ_id]='+type;
	var myAjax = new Ajax.Request( url, {method: 'post', parameters: pars, onComplete: alert('complete..')} );
}
//]]>
</script>

